package com.cs.pk.utils;

import java.io.IOException;
import java.io.OutputStream;

public class CmdOutStreamReader extends Thread {
	private OutputStream os = null;
    private String cmds[] = null;
    
    private int cmdToPrint = 0;
    
    private boolean dispose = false;
    
    private boolean exceptionOccured = false;
    private String exceptionMessage = null;
    private CmdInputStreamReader cmdInput = null;
    
	private Object LOCK = new Object();
	public CmdOutStreamReader(OutputStream os,CmdInputStreamReader is) {
		this.os = os;
		cmdInput = is;
	}
	
	
	public void run() {
		try {
			while(!dispose) {
				
					for (int i = 0; i < cmds.length; i++) {
							if(i==cmdToPrint) {
								try {
									Thread.sleep(100);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								cmdInput.setStartCaching(true);
							}
							os.write(cmds[i].getBytes());
						    os.flush();
					}
					synchronized (LOCK) {
						try {
							LOCK.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
			}
		} catch (IOException e) {
			exceptionMessage = e.getLocalizedMessage();
			exceptionOccured = true;
		}
	}
	
	
	
	public void setValues(String cmds[],int cmd2Prt) {
		cmdInput.setStartCaching(false);
		this.cmds = cmds;
		cmdToPrint = cmd2Prt;
		LOCK.notifyAll();
	}
	
}

